using Newtonsoft.Json;
using LiJ.Common.Core;
using LiJ.Common.Extention;
using LiJ.Http.Mos;
using LiJ.Framework.Official.Basic.Mos;
using System.Threading.Tasks;

namespace LiJ.Framework.Official.Basic
{
	/// <summary>
	///   二维码处理
	/// </summary>
	public class WxOffQrApi : WxOffBaseApi
	{
		/// <summary>
		/// 配置信息，如果这里不传，需要在程序入口静态 WxBaseApi.DefaultConfig 属性赋值
		/// </summary>
		/// <param name="config"></param>
		public WxOffQrApi(AppConfig config = null)
			: base(config)
		{
		}

		/// <summary>
		/// 获取二维码ticket
		/// </summary>
		/// <param name="req"></param>
		/// <returns></returns>
		public async Task<WxQrCodeResp> GetQrCodeTicketAsync(WxCreateSenceQrReq req)
		{
			LHttpRequest req2 = new LHttpRequest
			{
				HttpMothed = HttpMothed.Post,
				AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/qrcode/create",
				CustomBody = JsonConvert.SerializeObject(req)
			};
			return await base.RestCommonOffcialAsync<WxQrCodeResp>(req2, null);
		}

		/// <summary>
		/// 获取二维码图片地址
		/// </summary>
		/// <param name="ticket"></param>
		/// <returns></returns>
		public string GetQrCodeUrl(string ticket)
		{
			return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket.UrlEncode();
		}

		/// <summary>
		/// 生成短链
		/// </summary>
		/// <param name="longUrl">需要转换的长链接，支持http://、https://、weixin://wxpay 格式的url</param>
		/// <returns></returns>
		public async Task<WxShortUrlResp> GetShortUrl(string longUrl)
		{
			LHttpRequest req = new LHttpRequest
			{
				HttpMothed = HttpMothed.Post,
				AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/shorturl",
				CustomBody = string.Format("{{\"action\":\"long2short\",\"long_url\":\"{0}\"}}", longUrl)
			};
			return await base.RestCommonOffcialAsync<WxShortUrlResp>(req, null);
		}
	}
}
